﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using MainService.ZYPT;
using MainService;
using MainService.UISERVER;
using System.IO;
using System.Net;
using MainFrameDesign.ContentContorl;
using MainFrameDesign;
namespace StartMain
{
    public partial class MainFrm : Form
    {
        public MainFrm()
        {
            InitializeComponent();
            InitOther();
        }

        private void InitOther()
        {
            ImageList il = new ImageList();
            Image img = Image.FromFile(Application.StartupPath + "\\Images\\Normal.png");
            il.Images.Add("normal", img);
            ZYPTListView.SmallImageList = SysListView.SmallImageList = il;
            il = new ImageList();
            img = Image.FromFile(Application.StartupPath + "\\Images\\Solution.ico");
            il.Images.Add("solution",img);
            img = Image.FromFile(Application.StartupPath + "\\Images\\Page.ico");
            il.Images.Add("page", img);
            img = Image.FromFile(Application.StartupPath + "\\Images\\Select.ico");
            il.Images.Add("select", img);
            RightView.ImageList = il;
            TreeNode root = new TreeNode();
            root.Text = "应用系统";
            root.ImageKey = "solution";
            RightView.Nodes.Add(root);
            TreeNode pagenode = new TreeNode();
            pagenode.Text = "Text";
            pagenode.ImageKey = "page";
            root.Nodes.Add(pagenode);
            RightView.ExpandAll();
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            ProjectFrm pf = new ProjectFrm();
            if (pf.ShowDialog() == DialogResult.OK)
            {
                //LoadFrm loader = new LoadFrm();
                //SendOrPostCallback spc = null;
                ////如果当前编辑的是应用系统
                //if (MyServer.EditModel == EditModel.AppSystem)
                //{
                //    spc = new SendOrPostCallback(LoaderSolution);
                //}
                //else //当前编辑的是模版 
                //{
                    
                //}
                //loader.RegisterMesthod(spc, loader);
                //loader.ShowDialog();
                StartLoadZYLB();
                AddSysTool();
                CreateTestDesign();
            }
            else 
            {
                Application.Exit();
            }
        }
        /// <summary>
        /// 创建一个测试界面
        /// </summary>
        private void CreateTestDesign()
        {
            TabPage mainpage = new TabPage();
            CenterTab.Controls.Add(mainpage);
            DesignControl dc = new DesignControl();
            mainpage.Controls.Add(dc);
        }
        /// <summary>
        /// 添加系统组件
        /// </summary>
        private void AddSysTool()
        {
            this.SysListView.BeginUpdate();
            ListViewItem lvi = new ListViewItem();
            lvi.Text = "容器组件";
            lvi.Tag = new Div();
            lvi.ImageKey = "normal";
            this.SysListView.Items.Add(lvi);
            lvi = new ListViewItem();
            lvi.Text = "左右切分组件";
            lvi.Tag = new SplitLR();
            lvi.ImageKey = "normal";
            this.SysListView.Items.Add(lvi);
            this.SysListView.EndUpdate();
        }
        /// <summary>
        /// 开始装载解决方案
        /// </summary>
        private void LoaderSolution(object obj)
        {
            LoadFrm loader = obj as LoadFrm;
            try
            {
                loader.SetValueMess(10, "正在装载解决方案"); 
            }
            catch (Exception Err)
            {
                MessageBox.Show(Err.Message);
            }
            finally 
            {
                loader.Close();
            }
        }
        /// <summary>
        /// 开始装载资源类别
        /// </summary>
        private void StartLoadZYLB()
        {
            LoadFrm loader = new LoadFrm();
            SendOrPostCallback spc = new SendOrPostCallback(LoadZYLB);
            loader.RegisterMesthod(spc, loader);
            loader.ShowDialog();
        }

        void LoadZYLB(object obj) 
        {
            LoadFrm loader = obj as LoadFrm;
            try
            {
                loader.SetValueMess(10, "正在获取资源类别");
                ZYLB zylb = ZYPTServer.GetZYLB();
                loader.SetValueMess(20, "正在装载资源类别");
                TreeNode root = new TreeNode();
                root.Text = "资源类别";
                LeftTree.Nodes.Add(root);
                foreach (CataLog item in zylb.Childs)
                {
                    TreeNode node = new TreeNode();
                    root.Nodes.Add(node);
                    node.Text = item.Name;
                    node.Tag = item.ID;
                    AddChildLeftTree(item, node);
                }
                LeftTree.ExpandAll();
                LeftTree.SelectedNode = root;
            }
            catch (Exception Err)
            {
                MessageBox.Show(Err.Message);
                Application.Exit();
            }
            finally 
            {
                loader.Close();
                loader.Dispose();
            }
        }

        private void AddChildLeftTree(CataLog catalog, TreeNode root)
        {
            foreach (var item in catalog.Childs)
            {
                TreeNode node = new TreeNode();
                root.Nodes.Add(node);
                node.Text = item.Name;
                node.Tag = item.ID;
                AddChildLeftTree(item, node);
            }
        }

        private void LeftTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = LeftTree.SelectedNode;
            if (node.Tag != null&&node.Tag.GetType()!=typeof(ZJS))
            {
                    LoadFrm loader = new LoadFrm();
                    loader.MiddleValue =node;
                    SendOrPostCallback spc = new SendOrPostCallback(LoadCompoent);
                    loader.RegisterMesthod(spc, loader);
                    loader.ShowDialog();
            }
            else if (node.Tag != null && node.Tag.GetType() == typeof(ZJS))
            {
                ZJS zjs = node.Tag as ZJS;
                AddToolItem(zjs);
            }
        }

        private void LoadCompoent(object obj)
        {
            LoadFrm loader = obj as LoadFrm;
            try
            {
                loader.SetValueMess(20, "正在根据资源类别获取组件");
                TreeNode node = loader.MiddleValue as TreeNode;
                string sourceID = node.Tag.ToString();
                ZJS zjs = ZYPTServer.GetZuJian(sourceID);
                node.Tag = zjs;
                loader.SetValueMess(40, "正在获取组件图片");
                string url = "http://" + MyServer.ServerConfig.ZYPT.ServerIP + ":" + MyServer.ServerConfig.ZYPT.ServerPort;
                WebClient webclient = new WebClient();
                foreach (ZJ item in zjs.Childs)
                {
                    if (!string.IsNullOrEmpty(item.ImageUrl))
                    {
                        string urlpath = url + item.ImageUrl;
                        string filename =Application.StartupPath+"\\Images\\"+Path.GetFileName(urlpath);
                        if (!File.Exists(filename))
                        {
                            webclient.DownloadFile(urlpath, filename);
                        }
                        item.Image = Image.FromFile(filename);
                    }
                }
                loader.SetValueMess(80, "正在装载组件");
                AddToolItem(zjs);
            }
            catch (Exception Err)
            {
                MessageBox.Show(Err.Message);
            }
            finally
            {
                loader.Close();
            }
        }

        private void AddToolItem(ZJS zjs)
        {
            this.ZYPTListView.Items.Clear();
            this.ZYPTListView.BeginUpdate();
            foreach (var item in zjs.Childs)
            {
                if (item.Image != null)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.Text = item.ChinaName;
                    lvi.Tag = item;
                    lvi.ImageKey = "normal";
                    this.ZYPTListView.Items.Add(lvi);
                }
            }
            this.ZYPTListView.EndUpdate();
        }

        private void RightView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = e.Node;
            if (node.Text != "应用系统") 
            {
                node.SelectedImageKey = "select";
            }
        }
    }
}
